<template>
    <el-dialog v-model="dialogVisible" :show-close="false" width="800px">
        <template #header>
            <div class="flex flex-jc-sb">
                <div>
                    {{ companyName }}
                </div>
                <div class="flex items-center">
                    <el-button type="primary" @click="handleExport">导出列表</el-button>
                    <el-icon class="mg-l-20 pointer" :size="24" @click="handleClose">
                        <Close />
                    </el-icon>
                </div>
            </div>
        </template>
        <div>
            <tableComponent :table-header="tableHeader" :table-data="tableData" no-pagination>
                <template #operate>
                    <el-table-column width="70" label="操作" fixed="right">
                        <template #default="scope">
                            <el-button v-if="scope.row.typeName !== '团意险'" type="danger" :disabled="scope.row.status === 1 || !scope.row.companyId" link
                                @click="handleClick(scope.row)">拒保</el-button>
                        </template>
                    </el-table-column>
                </template>
            </tableComponent>
        </div>
    </el-dialog>
</template>
<script setup>
import { ref, watch } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import tableComponent from '@/components/tableComponent/index.vue'
import { coClaimDetail, exportCoClaimDetail } from '@/api/tools/compensateRate.ts'
import { longsetadminapi, datinsureadminsetapi } from '@/api/api.ts'
import baseurl from '@/api/env'

const props = defineProps({
    show: {
        type: Boolean,
        default: false
    },
    companyId: {
        type: String,
        default: ''
    },
    companyName: {
        type: String,
        default: ''
    }
})

const emits = defineEmits(['update:show'])

const dialogVisible = ref(false)

const tableData = ref([])

const map = {
    periodInsur: '长期保',
    dailyInsur: '日日保',
    tyInsur: '团意险'
}

const tableHeader = ref([
    {
        label: '保险类型',
        prop: 'typeName',
    },
    {
        label: '案件总数',
        prop: 'finalAmount',
    },
    {
        label: '结案金额',
        prop: 'finalMoney',
    },
    {
        label: '实际保费',
        prop: 'totalPrice',
    },
    {
        label: '理赔率',
        prop: 'claimRate',
    },
    {
        label: '操作',
        prop: 'operate'
    }
])

const getList = () => {
    coClaimDetail({
        companyId: props.companyId,
    }).then(res => {
        for (const key in res) {
            if (!res[key]) {
                res[key] = {}
            }
            res[key].typeName = map[key]
        }
        tableData.value = Object.values(res)
    })
}

const handleExport = () => {
    exportCoClaimDetail({
        companyId: props.companyId
    }).then(res => {
        window.location.href = baseurl + '/common/download?delete=true&fileName=' + res
    })
}

const handleClose = () => {
    dialogVisible.value = false
}

const handleClick = (row) => {
    if (row.typeName === '长期保') {
        ElMessageBox.confirm(`是否拒保 ${row.companyName} 长期保？`, '提示', {
            type: 'error'
        }).then(() => {
            longsetadminapi({
                companyId: row.companyId,
                refusalGuaranteeStatus: 1,
                userType: 2
            }).then(() => {
                getList()
            })
        })
    } else if (row.typeName === '日日保') {
        ElMessageBox.confirm(`是否拒保 ${row.companyName} 日日保？`, '提示', {
            type: 'error'
        }).then(() => {
            datinsureadminsetapi({
                companyId: row.companyId,
                refusalGuaranteeStatus: 1,
                blockedAccount: 1
            }).then(() => {
                getList()
            })
        })
    }
}

watch(() => props.show, (newval) => {
    dialogVisible.value = newval
    if (newval) {
        getList()
    }
})

watch(dialogVisible, (newval) => {
    emits('update:show', newval)
})

</script>
<style lang="scss" scoped>
::v-deep(.el-dialog__header) {
    margin-right: 0 !important;
}
</style>